package com.aaa.system.Controller.Api;

import com.aaa.entity.Role;
import com.aaa.entity.RoleMenu;
import com.aaa.system.aop.annotation.Log;
import com.aaa.system.service.RoleService;
import com.aaa.system.vo.RoleVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("system/api/role")
public class ApiRoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询用户的角色和  角色表
     * @return
     */
    @GetMapping("getByUserId/{userId}")
    public Result<Map<String,Object>> getByUserId(@PathVariable Long userId){
        return roleService.findUserRoleByUserId(userId);
    }
    /**
     * 分配用户的角色
     */
    @PostMapping("confirmRole")
    @Log(title = "确认分配角色",business_type = "2")
    public Result<String> confirmRole(Long userId,Long[] roleIds){
        return roleService.shareRole(userId,roleIds);
    }

    /**
     * 查询所有的role表单信息
     * @param roleVo
     * @return
     */
    @PostMapping("list")
    public Result<IPage<Role>> getListRole(@RequestBody RoleVo roleVo){
        return roleService.findAllData(roleVo);
    }

    /**
     * 添加或者修改角色信息
     */
    @PostMapping("saveOrUpdateRole")
    @Log(title = "添加/修改角色信息",business_type = "1")
    public Result<Boolean> saveOrUpdate(@RequestBody Role role){
        return roleService.saveOrUpdateRole(role);
    }

    /**
     *删除一条角色的信息
     */
    @DeleteMapping("delRole/{roleId}")
    @Log(title = "删除角色信息",business_type = "3")
    public Result<Boolean> delRole(@PathVariable Long roleId){
        //删除 角色是否有菜单 有菜单不能删除 没有菜单  可以删除
        List<RoleMenu> list =roleService.findMenuByRoleId(roleId);
        if (list.size()>0){
            return new Result<>(200,"角色有菜单不能删除",true);//有菜单 不能删除
        }
        return roleService.delOneRole(roleId);
    }
    /**
     * 批量删除
     */
    @DeleteMapping("bachUser/{roleIds}")
    @Log(title = "批量删除角色信息",business_type = "3")
    public Result<Boolean> bachRole(@PathVariable Long[] roleIds){
        return roleService.bachRole(roleIds);
    }
}
